package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomapartmentarea.vo.FuzzyQueryByAreaBuildingUnitVO;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomApartmentAreaMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomApartmentArea;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author hans.zhang
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RoomApartmentAreaRepository extends ServiceImpl<RoomApartmentAreaMapper, RoomApartmentArea> {
    private final RoomApartmentAreaMapper roomApartmentAreaMapper;

    public List<RoomApartmentArea> getXqList(List<Long> ids) {
        LambdaQueryWrapper<RoomApartmentArea> wrapper = Wrappers.lambdaQuery();

        wrapper.select(RoomApartmentArea::getId,RoomApartmentArea::getArea)
                .in(RoomApartmentArea::getApartmentId,ids);

        return roomApartmentAreaMapper.selectList(wrapper);

    }

    public RoomApartmentArea myGetById(Long apartmentAreaId) {
        RoomApartmentArea roomApartmentArea = this.getById(apartmentAreaId);
        Assert.isTrue(ObjectUtil.isNotEmpty(roomApartmentArea), "小区信息不存在：id{}", apartmentAreaId);
        return roomApartmentArea;
    }

    public RoomApartmentArea getByAddressAndBuilding(String province, String city, String district, String street, String address, String areaName, String buildingNo, String unitNo) {
        LambdaQueryWrapper<RoomApartmentArea> wrapper = Wrappers.lambdaQuery();
        wrapper.select(RoomApartmentArea::getId, RoomApartmentArea::getFloorNum, RoomApartmentArea::getGroundFloorNum, RoomApartmentArea::getFloorRoomNum)
                .eq(StrUtil.isNotEmpty(province), RoomApartmentArea::getProvince, province)
                .eq(StrUtil.isNotEmpty(city),RoomApartmentArea::getCity, city)
                .eq(StrUtil.isNotEmpty(district),RoomApartmentArea::getDistrict, district)
                .eq(StrUtil.isNotEmpty(street),RoomApartmentArea::getStreet, street)
                .eq(StrUtil.isNotEmpty(address),RoomApartmentArea::getAddress, address)
                .eq(StrUtil.isNotEmpty(areaName),RoomApartmentArea::getArea, areaName)
                .eq(StrUtil.isNotEmpty(buildingNo),RoomApartmentArea::getBuildingNo, buildingNo)
                .eq(StrUtil.isNotEmpty(unitNo),RoomApartmentArea::getUnitNo, unitNo);
        return this.getOne(wrapper);
    }

    public List<RoomApartmentArea> getByAddressAndIsConcentrate(RoomHouse roomHouse) {
        return roomApartmentAreaMapper.getByAddressAndIsConcentrate(roomHouse);
    }

    /**
     * 根据小区楼栋单元名模糊查询楼栋单元信息
     * @param searchBox
     * @param apartmentId
     * @return
     */
    public List<FuzzyQueryByAreaBuildingUnitVO> fuzzyQueryByAreaBuildingUnit(String searchBox, Long apartmentId) {
        return roomApartmentAreaMapper.fuzzyQueryByAreaBuildingUnit(searchBox, apartmentId);
    }
}
